﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevComponents.DotNetBar;

namespace Quan_Ly_Dai_Ly
{
    public partial class BaoCaoDoanhSo : Office2007Form
    {
        public BaoCaoDoanhSo()
        {
            InitializeComponent();
        }

        private void BaoCaoDoanhSo_Load(object sender, EventArgs e)
        {
            int[] Thang = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; 
            cb_Thang.DataSource = Thang;
            int[] Nam = new int[100];
            Nam[0] = DateTime.Now.Year;
            for (int i = 1; i < 100; ++i)
                Nam[i] = Nam[i - 1] - 1;
            cb_Nam.DataSource = Nam;
        }

        private void bt_HienThi_Click(object sender, EventArgs e)
        {
            try
            {
                int result = MainForm.ExecProcedure("sp_BaoCaoDoanhSo",
                                                    new string[] { "@Thang", "@Nam" },
                                                    new object[] { cb_Thang.Text, cb_Nam.Text });
                if (result == 1)
                {
                    dt.Clear();
                    tb_Tong.ResetText();
                    MessageBoxEx.Show("Không có doanh thu từ đại lý nào trong tháng này.", "Thông Báo");
                }
                else
                    if (result != -1)
                    {
                        MainForm.Connect.Open();
                        string query = "select * from f_BaoCaoDoanhSo(" + cb_Thang.Text + ", " + cb_Nam.Text + ")";
                        SqlDataAdapter da = new SqlDataAdapter(query, MainForm.Connect);
                        dt.Clear();
                        da.Fill(dt);
                        MainForm.Connect.Close();
                        dg_DoanhSo.DataSource = dt;
                        STT.DataPropertyName = "STT";
                        DaiLy.DataPropertyName = "DaiLy";
                        SoPhieuXuat.DataPropertyName = "SoPhieuXuat";
                        TongTriGia.DataPropertyName = "TongTriGia";
                        TyLe.DataPropertyName = "TyLe";
                        tb_Tong.Text = result.ToString();
                    }
            }
            catch (SqlException sqlEx)
            {
                MainForm.Connect.Close();
                MessageBoxEx.Show("Lỗi kết nối cơ sở dữ liệu!\n" + sqlEx.Message, "Thông Báo");
            }
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            Close();
        }

        DataTable dt = new DataTable();
    }
}
